package com.bgxt.datatimepickerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpinnerAdapterActivity extends Activity {
	private Spinner spinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinneradapter);

		spinner = (Spinner) findViewById(R.id.spinnerAdapter);
		//声明一个SimpleAdapter独享，设置数据与对应关系
		SimpleAdapter simpleAdapter = new SimpleAdapter(
				SpinnerAdapterActivity.this, getData(), R.layout.items,
				new String[] { "ivLogo", "applicationName" }, new int[] {
						R.id.imageview, R.id.textview });
		//绑定Adapter到Spinner中
		spinner.setAdapter(simpleAdapter);
		//Spinner被选中事件绑定。
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				//parent为一个Map结构的和数据
				Map<String, Object> map = (Map<String, Object>) parent
						.getItemAtPosition(position);
				Toast.makeText(SpinnerAdapterActivity.this,
						map.get("applicationName").toString(),
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}

	public List<Map<String, Object>> getData() {
		//生成数据源
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		//每个Map结构为一条数据，key与Adapter中定义的String数组中定义的一一对应。
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ivLogo", R.drawable.bmp1);
		map.put("applicationName", "表情1");
		list.add(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("ivLogo", R.drawable.bmp2);
		map2.put("applicationName", "表情2");
		list.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("ivLogo", R.drawable.bmp3);
		map3.put("applicationName", "表情3");
		list.add(map3);
		return list;
	}
}
